package site.shader85.Kingdoms.inventory.gui;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;

import com.google.common.collect.Lists;

import net.md_5.bungee.api.ChatColor;
import site.shader85.Kingdoms.Utils;
import site.shader85.Kingdoms.inventory.AbstractGUI;
import site.shader85.Kingdoms.kingdom.Kingdom;

public class KingdomJoinerGUI extends AbstractGUI {

	private int page = 1;
	private int size = 1;
	private List<Kingdom> kingdoms;

	public KingdomJoinerGUI() {
		super(54, ChatColor.DARK_PURPLE + "&l加入王国", false);
		kingdoms = Lists.newArrayList();
		kingdoms.addAll(Kingdom.getKingdoms());
		int size = kingdoms.size();
		this.size = size / 45 + 1;
		refresh();
	}

	@Override
	protected ItemStack index(int slot) {
		if (slot < 54) {
			int id = (page - 1) * 54 + slot;
			if (id + 1 <= kingdoms.size()) {
				Kingdom k = kingdoms.get(id);
				String owner = k.getOwner();
				ArrayList<String> list = Lists.newArrayList();
				list.add(ChatColor.RESET + "" + ChatColor.GRAY + "君主:" + owner);
				list.add(ChatColor.RESET + "" + ChatColor.GRAY + "辖下领地:" + k.getDomainSize() + "个");
				list.add(ChatColor.RESET + "" + ChatColor.GRAY + "国土面积:" + k.getArea() + "m²");
				list.add(ChatColor.RESET + "" + ChatColor.GRAY + "国土空间:" + k.getV() + "m³");
				return createSkull("&e&l" + k.getName(), owner, list);
			}
		}
		if (slot == 45)
			return create(Material.GOLD_INGOT, "&e上一页", null, false);
		if (slot == 49)
			return create(Material.PAPER, "&e第" + page + "页,共" + size + "页", null, false);
		if (slot == 53)
			return create(Material.GOLD_INGOT, "&e下一页", null, false);
		return null;
	}

	@Override
	public boolean onClick(int slot, ClickType type, HumanEntity player) {
		if (slot < 54 && slot >= 0) {
			int id = (page - 1) * 54 + slot;
			if (id + 1 <= kingdoms.size()) {
				Kingdom k = kingdoms.get(id);
				k.addCitizen(player.getName());
				player.closeInventory();
				AbstractGUI.views.remove(inventory);
				player.sendMessage(Utils.toMessage(ChatColor.AQUA+"[K]&e你加入了 " + ChatColor.BLUE + k.getName()));
			}
		}
		if (slot == 45 && page != 1) {
			page -= 1;
			refresh();
		}

		if (slot == 53 && page < size) {
			page += 1;
			refresh();
		}
		return true;
	}

}
